package com.party.business.controller;


import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollectionUtil;
import com.party.business.domain.TBusinessDept;
import com.party.business.domain.TBusinessPost;
import com.party.business.domain.TFjFile;
import com.party.business.domain.TKc;
import com.party.business.service.ITBusinessDeptService;
import com.party.business.service.ITBusinessPostService;
import com.party.business.service.ITFjFileService;
import com.party.business.service.ITKcService;
import com.party.common.annotation.Log;
import com.party.common.core.controller.BaseController;
import com.party.common.core.domain.AjaxResult;
import com.party.common.core.page.TableDataInfo;
import com.party.common.enums.BusinessType;
import com.party.common.utils.StringUtils;
import com.party.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 课程信息Controller
 *
 * @author ruoyi
 * @date 2024-11-08
 */
@RestController
@RequestMapping("/business/kcxx")
public class TKcController extends BaseController {
    @Autowired
    private ITKcService tKcService;

    @Autowired
    private ITBusinessPostService businessPostService;

    @Autowired
    private ITBusinessDeptService  businessDeptService;

    /**
     * 查询课程信息列表
     */
//@RequiresPermissions("system:kc:list")
    @GetMapping("/list")
    public TableDataInfo list(TKc tKc) {
        startPage();
        List<TKc> list = tKcService.selectTKcList(tKc);
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    //@RequiresPermissions("system:kc:export")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TKc tKc) {
        List<TKc> list = tKcService.selectTKcList(tKc);
        ExcelUtil<TKc> util = new ExcelUtil<TKc>(TKc.class);
        util.exportExcel(response, list, "课程信息数据");
    }

    /**
     * 获取课程信息详细信息
     */
    //@RequiresPermissions("system:kc:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(tKcService.selectTKcById(id));
    }

    /**
     * 新增课程信息
     */
    //@RequiresPermissions("system:kc:add")
//    @Log(title = "课程信息", businessType = BusinessType.INSERT)
//    @PostMapping
//    @Transactional
//    public AjaxResult add(@RequestBody TKc tKc) throws Exception {
//        boolean save = tKcService.save(tKc);
//        List<Long> postList = tKc.getPostList();
//        if(StringUtils.isEmpty(postList)){
//            throw  new Exception("请选择岗位");
//        }
//        for (int i = 0; i < postList.size(); i++) {
//            Long s = postList.get(i);
//            TBusinessPost  businessPost=new TBusinessPost();
//            businessPost.setBusinessId(tKc.getId());
//            businessPost.setPostId(s+"");
//            businessPostService.save(businessPost);
//        }
//
//        return success();
//    }


    @Autowired
    private ITFjFileService tFjFileService;

    /**
     * 修改课程信息
     */
    //@RequiresPermissions("system:kc:edit")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody TKc tKc) throws Exception {

        TFjFile  fjFile=new TFjFile();
        fjFile.setBusinessId(tKc.getId());
        List<TFjFile> tFjFiles = tFjFileService.selectTFjFileList(fjFile);
        if(CollectionUtil.isEmpty(tFjFiles)){
            throw  new Exception("请上传视频");
        }
        tKcService.saveOrUpdate(tKc);
        tKcService.updatePostAndDept(tKc);
        return success();
    }

    /**
     * 删除课程信息
     */
    //@RequiresPermissions("system:kc:remove")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tKcService.removeByIds(Arrays.asList(ids)));
    }
}
